package com.example.csupermaket.service.impl;

import com.example.csupermaket.exception.ServiceException;
import com.example.csupermaket.mapper.BannerMapper;
import com.example.csupermaket.pojo.dto.BannerAddNewDTO;
import com.example.csupermaket.pojo.dto.BannerUpdateDTO;
import com.example.csupermaket.pojo.entity.Banner;
import com.example.csupermaket.pojo.vo.BannerStandardVO;
import com.example.csupermaket.pojo.vo.ProductStandardVO;
import com.example.csupermaket.service.IBannerService;
import com.example.csupermaket.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class BannerServiceImpl implements IBannerService {

    @Autowired
    private BannerMapper mapper;

    @Override
    public void insert(BannerAddNewDTO bannerAddNewDTO) {


        if (bannerAddNewDTO.getBurl()==null){
            String message = "添加失败，请选择图片！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerAddNewDTO,banner);
        mapper.insert(banner);
    }

    @Override
    public void deleteById(int bid) {

        BannerStandardVO queryResult = mapper.selectById(bid);
        if (queryResult==null){
            String message = "删除失败，尝试删除的轮播图不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        mapper.deleteById(bid);

    }

    @Override
    public void update(int bid,BannerUpdateDTO bannerUpdateDTO) {

        BannerStandardVO queryResult = mapper.selectById(bid);

        if (queryResult==null){
            String message = "删除失败，尝试修改的轮播图不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerUpdateDTO,banner);
        banner.setBid(bid);
        mapper.update(banner);
    }

    @Override
    public String selectUrlById(int bid) {
        String queryResult = mapper.selectUrlById(bid);
        if (queryResult == null) {
            String message = "获取轮播图失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }

    @Override
    public BannerStandardVO selectById(int bid) {

        BannerStandardVO queryResult = mapper.selectById(bid);
        if (queryResult == null) {
            String message = "获取轮播图失败，尝试访问的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        return queryResult;
    }
}
